<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\JsExpression;
/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessPigPrice */
/* @var $form yii\widgets\ActiveForm */


//成本总计
$total = 0;
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <?php $form = ActiveForm::begin([
                'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                'fieldConfig' => [
                    'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                ]
            ]); ?>
            <div class="adv-table editable-table ">
                <div class="business-pig-price-form">


                            <?= $form->field($model, 'number')->textInput(['readonly'=>true,'value'=>date('YmdHis').rand(10000,99999)]) ?>

                            <?php if($model->isNewRecord) { ?>

                                <?= $form->field($model, 'person')->textInput(['type'=>'text','readonly'=>true,'value'=>Yii::$app->user->identity->username]) ?>

                            <?php } else { ?>
                                <?= $form->field($model, 'personid')->widget(\kartik\select2\Select2::className(), [
                                    'options' => ['placeholder' => '请输入上报人姓名 ...'],
                                    'initValueText'=>$model->person,
                                    'pluginOptions' => [
                                        'allowClear' => true,
                                        'minimumInputLength' => 1,//重要
                                        'language' => [
                                            'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                                        ],
                                        'ajax' => [
                                            'url' => '/business/search/search-person',
                                            'dataType' => 'json',
                                            'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                            'cache' => true
                                        ],
                                        'width' => '400px',
                                        'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                        'templateResult' => new JsExpression('function(res) { return res.text; }'),
                                        'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                                    ],
                                ]);
                                ?>
                            <?php } ?>



                            <?= $form->field($model, 'pigid')->widget(\kartik\select2\Select2::className(), [
                                'options' => ['placeholder' => '请输入仔猪编号...'],
                                'initValueText'=>$model->pigno,
                                'pluginOptions' => [
                                    'allowClear' => true,
                                    'minimumInputLength' => 1,//重要
                                    'language' => [
                                        'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                                    ],
                                    'ajax' => [
                                        'url' => '/business/search/search-pigfg',
                                        'dataType' => 'json',
                                        'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                        'cache' => true
                                    ],
                                    'width' => '400px',
                                    'readonly' => true,
                                    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                    'templateResult' => new JsExpression('function(res) { return res.text; }'),
                                    'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                                ],
                            ]);
                            ?>


                            <?= $form->field($model, 'grossweight')->textInput(['type'=>'number','readonly'=>true]) ?>


                            <?= $form->field($model, 'netweight')->textInput(['type'=>'number']) ?>

                            <?= $form->field($model, 'fgcost')->textInput(['type'=>'number']) ?>

                </div>
            </div>

            <div class="row">
                <div class="col-md-10 col-md-offset-1">
                    <table class="table table-striped" id="cost">
                        <tr>
                            <th style="width: 10%">分割类型</th>
                            <th style="width: 10%">单价</th>
                            <th style="width: 10%">销售单位</th>
                            <th style="width: 10%">重量</th>
                            <th style="width: 20%">备注</th>
                            <th style="width: 10%">操作</th>
                        </tr>
                        <?php foreach ($model->detail as $item => $value)  { ?>
                            <?php $total += $value['price'] ?>
                        <tr class="costdata">
                            <input type="hidden" name="id[]" value="<?= $value['id'] ?>">
                            <td>
                                <select type="text" class="form-control" name="type[]">
                                    <?php foreach ($type as $key => $row)  { ?>
                                        <option value="<?= $row['name'] ?>" <?php if($value['type'] == $row['name']) { ?> selected <?php } ?>  ><?= $row['name'] ?></option>
                                    <?php } ?>
                                </select>
                            </td>
                            <td><input type="number" class="form-control" value="<?= $value['price'] ?>" name="price[]"></td>
                            <td>
                                <select type="text" class="form-control" name="unit[]">
                                    <?php foreach ($unit as $key => $row)  { ?>
                                        <option value="<?= $row['name'] ?>" <?php if($value['unit'] == $row['name']) { ?> selected <?php } ?>><?= $row['name'] ?></option>
                                    <?php } ?>
                                </select>
                            </td>
                            <td><input type="number" class="form-control" value="<?= $value['weight'] ?>" name="weight[]"></td>
                            <td><input type="text" class="form-control" value="<?= $value['otherdesc'] ?>" name="otherdesc[]"></td>
                            <td>
                                <div style="float: left;width: 13%;margin-left: 1%">
                                    <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?>
                                </div>
                            </td>
                        </tr>
                        <?php } ?>

                        <tr class="costdata" id="zongji">
                            <td><b>总计</b></td>
                            <td><b><?= $total ?></b></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </table>

                    <div class="form-group">
                        <?= Html::button('添加一项明细',['id'=>'ccopy','class'=>'btn btn-info']) ?>
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success sub' : 'btn btn-primary sub']) ?>
                    </div>

                </div>
            </div>
            <?php ActiveForm::end(); ?>
        </div>
    </section>
</div>
</div>


<script>

    <?php $this->beginBlock('js_end') ?>

    $(document).ready(function() {

        $('input[name="pig[]"]').keyup(function () {
            shishiSearch(this)
        });

        var str = '<tr class="costdata"> <td> <select type="text" class="form-control" name="type[]"> <?php foreach ($type as $key => $row)  { ?> <option value="<?= $row['name'] ?>"><?= $row['name'] ?></option> <?php } ?> </select> </td> <td><input type="number" class="form-control" value="" name="price[]"></td> <td> <select type="text" class="form-control" name="unit[]"> <?php foreach ($unit as $key => $row)  { ?> <option value="<?= $row['name'] ?>"><?= $row['name'] ?></option> <?php } ?> </select> </td> <td><input type="number" class="form-control" value="" name="weight[]"></td> <td><input type="text" class="form-control" value="" name="otherdesc[]"></td> <td> <div style="float: left;width: 13%;margin-left: 1%"> <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?> </div> </td> </tr>';
        //添加一项
        $('#ccopy').click(function () {
            $('#zongji').before(str);
            $('.del').unbind('click').click(function () {
                if (confirm('确定要删除吗？')) {
                    $(this).parent().parent().parent('.costdata').remove();
                    return;
                }
            });
        });

        //删除一项
        $('.del').unbind('click').click(function () {
            if (confirm('确定要删除吗？')) {
                $(this).parent().parent().parent('.costdata').remove();
            }
        });

    });

    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>

</script>


<script>
    <?php $this->beginBlock('js_end') ?>

    $('#businesspigprice-pigid').change(function(){
//            console.log($(this).val());
        $.get('/business/pig-price/search-gweight',{pigid:$(this).val()},function(res){
            $('#businesspigprice-grossweight').val(res.data)
        },'json')
    })
    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>